package com.springboot.usercenter.controller;


import com.springboot.usercenter.model.UserInfo;
import com.springboot.usercenter.model.dto.DeleteUserDTO;
import com.springboot.usercenter.model.dto.SearchUserListDTO;
import com.springboot.usercenter.model.dto.UserLoginDTO;
import com.springboot.usercenter.model.dto.UserRegisterDTO;
import com.springboot.usercenter.model.request.DeleteUserParam;
import com.springboot.usercenter.model.request.SearchUserListParam;
import com.springboot.usercenter.model.request.UserLoginParam;
import com.springboot.usercenter.model.request.UserRegisterParam;
import com.springboot.usercenter.model.result.Result;
import com.springboot.usercenter.service.UserService;
import com.springboot.usercenter.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter;
import org.apache.catalina.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-04-23
 * Time: 13:54
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public Result userRegister(@RequestBody UserRegisterParam userRegisterParam) {
        String userAccount = userRegisterParam.getUserAccount();
        String userPassword = userRegisterParam.getUserPassword();
        String checkPassword = userRegisterParam.getCheckPassword();
        if (!StringUtils.hasText(userAccount) || !StringUtils.hasText(userPassword)
                || !StringUtils.hasText(checkPassword)) {
            return Result.error("参数不合法! 账号或密码或确认密码为空");
        }
        UserRegisterDTO result = userService.userRegister(userAccount, userPassword, checkPassword);
        if (result.getId() == -1) {
            return Result.error(result.getMessage());
        }
        return Result.success(result.getId(), result.getMessage());
    }

    // 用户登录
    @PostMapping("/login")
    public Result userLogin(@RequestBody UserLoginParam userLoginParam,
                            HttpServletRequest request) {
        String userAccount = userLoginParam.getUserAccount();
        String userPassword = userLoginParam.getUserPassword();
        if (!StringUtils.hasText(userAccount) || !StringUtils.hasText(userPassword)) {
            return Result.error("参数不合法! 账号或密码为空");
        }
        UserLoginDTO result = userService.userLogin(userAccount, userPassword, request);
        if (result.getUserInfo() == null) {
            // 说明登录失败
            return Result.error(result.getMessage());
        }
        return Result.success(result.getUserInfo(), result.getMessage());
    }

    // 超级管理员根据用户名查询出所有符合条件的用户
    @GetMapping("/search")
    public Result searchUserList(@RequestBody SearchUserListParam searchUserListParam,
                                         HttpServletRequest request) {
        String username = searchUserListParam.getUsername();
        if (!StringUtils.hasText(username)) {
            return Result.error("参数不合法! 用户名为空");
        }
        SearchUserListDTO result = userService.searchUserList(username, request);
        if (result.getUserInfoList() == null) {
            return Result.error(result.getMessage());
        }
        return Result.success(result.getUserInfoList(), result.getMessage());
    }

    // 超级管理员根据Id去删除对应的用户（这里是逻辑删除，而不是物理删除）
    // 通过 配置 Mybatis-plus 进行逻辑删除
    @PostMapping("/delete")
    public Result deleteUser(@RequestBody DeleteUserParam deleteUserParam,
                             HttpServletRequest request) {
        Long id = deleteUserParam.getId();
        if (id == null) {
            return Result.error("参数不合法! id为空");
        }
        if (id <= 0) {
            return Result.error("参数不合法! id <= 0");
        }
        DeleteUserDTO result = userService.deleteUser(id, request);
        if (!result.isDelete()) {
            return Result.error(result.getMessage());
        }
        return Result.success(result.getMessage());
    }
}
